/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package commonClasses.Messages;

import commonClasses.*;
import commonClasses.commonInterfaces.MsgIntf;
import commonClasses.commonInterfaces.ProtocolIntf;

/**
 *
 * @author Pozytywny!
 */
public class QueryMsg extends Msg implements MsgIntf{

    public long getItemID() {
        return _itemID;
    }

    private long _itemID = -1;

    public QueryMsg(ProtocolIntf protocol, long itemID) {
        super(protocol, MagicNumbers.QUERY_MSG_NR);
        _itemID = itemID;
    }

    public QueryMsg(ProtocolIntf protocol) {
        super(protocol, MagicNumbers.QUERY_MSG_NR);

    }

    public void receive() throws Exception {
        //msg id już wczytany

        long dataLenth = receiveUint();
        Utils.received(dataLenth);
        if(dataLenth != 4l) throw new Exception("długość danych: " + dataLenth +
                ", oczekiwana: 4");

        long itemID = receiveUint();
        Utils.received("received item id:" + itemID);
    }

    public void send() throws Exception {
        if(_itemID == -1)
            throw new Exception("itemID nie został zainicjalizowany");
        /*
         * najpierw msg id
         * potem leci data...
         * czyli:
         * liczba 4 (bo prześlemy 4 bajty)
         * właściwe cztery bajty z item ID
         */

        int mesgId[] = this.idToIntArr();
        sendIntArr(mesgId);

        int dataLength[] = longToArr(4l);
        sendIntArr(dataLength);

        int itemID[] = longToArr(_itemID);
        sendIntArr(itemID);
        
    }

}
